/*****************************************************************************

FileName: 	<BitmapDecode.cpp>

File Description:
Converts uncompressed BMP file into YUV420 file

*****************************************************************************/


#include "dbmpafx.h"


/*****************************************************************************

Routine: ReadBitmapFileHeader

returns        	: void
Description			: Reads BITMAPFILEHEADER
*****************************************************************************/
void ReadBitmapFileHeader(bmpCodecObject *ptrbitmap, FILE *pbmpfile)
{

	UINT16 tmp16;
	UINT32 tmp32;

	fread(&tmp16 ,sizeof(tmp16), 1, pbmpfile); (ptrbitmap->bmfh).bfType = tmp16;
	fread(&tmp32 ,sizeof(tmp32), 1, pbmpfile); (ptrbitmap->bmfh).bfSize = tmp32;
	fread(&tmp16 ,sizeof(tmp16), 1, pbmpfile); (ptrbitmap->bmfh).bfReserved1 = tmp16;
	fread(&tmp16 ,sizeof(tmp16), 1, pbmpfile); (ptrbitmap->bmfh).bfReserved2 = tmp16;
	fread(&tmp32 ,sizeof(tmp32), 1, pbmpfile); (ptrbitmap->bmfh).bfOffbits = tmp32;

#ifdef TRACE
	printf("\n\nBITMAPFILEHEADER:");
	printf("\n\tbfType      	\t%d", 	(ptrbitmap->bmfh).bfType);
	printf("\n\tbfSize      	\t%d", 	(ptrbitmap->bmfh).bfSize);
	printf("\n\tbfReserved1 	\t%d", 	(ptrbitmap->bmfh).bfReserved1);
	printf("\n\tbfReserved2 	\t%d", 	(ptrbitmap->bmfh).bfReserved2);
	printf("\n\tbfOffbits   	\t%d", 	(ptrbitmap->bmfh).bfOffbits);
#endif

}


/*****************************************************************************

Routine: ReadBitmapInfoHeader

returns        	: void
Description			: Reads BITMAPInfoHEADER
*****************************************************************************/
void ReadBitmapInfoHeader(bmpCodecObject *ptrbitmap, FILE *pbmpfile)
{

	UINT16 tmp16;
	UINT32 tmp32;

	fread(&tmp32 ,sizeof(tmp32), 1, pbmpfile); (ptrbitmap->bmih).biSize = tmp32;
	fread(&tmp32 ,sizeof(tmp32), 1, pbmpfile); (ptrbitmap->bmih).biWidth = tmp32;
	fread(&tmp32 ,sizeof(tmp32), 1, pbmpfile); (ptrbitmap->bmih).biHeight = tmp32;
	fread(&tmp16 ,sizeof(tmp16), 1, pbmpfile); (ptrbitmap->bmih).biPlanes = tmp16;
	fread(&tmp16 ,sizeof(tmp16), 1, pbmpfile); (ptrbitmap->bmih).biBitCount = tmp16;
	fread(&tmp32 ,sizeof(tmp32), 1, pbmpfile); (ptrbitmap->bmih).biCompression = tmp32;
	fread(&tmp32 ,sizeof(tmp32), 1, pbmpfile); (ptrbitmap->bmih).biSizeImage = tmp32;
	fread(&tmp32 ,sizeof(tmp32), 1, pbmpfile); (ptrbitmap->bmih).biXPelsPerMeter = tmp32;
	fread(&tmp32 ,sizeof(tmp32), 1, pbmpfile); (ptrbitmap->bmih).biYPelsPerMeter = tmp32;
	fread(&tmp32 ,sizeof(tmp32), 1, pbmpfile); (ptrbitmap->bmih).biClrUsed = tmp32;
	fread(&tmp32 ,sizeof(tmp32), 1, pbmpfile); (ptrbitmap->bmih).biClrImportant = tmp32;

#ifdef TRACE
	printf("\n\nBITMAPINFOHEADER:");
	printf("\n\tbiSize      	\t%d", 	(ptrbitmap->bmih).biSize);
	printf("\n\tbiWidth         \t%d", 	(ptrbitmap->bmih).biWidth);
	printf("\n\tbiHeight 	    \t%d", 	(ptrbitmap->bmih).biHeight);
	printf("\n\tbiPlanes 	    \t%d", 	(ptrbitmap->bmih).biPlanes);
	printf("\n\tbiBitCount   	\t%d", 	(ptrbitmap->bmih).biBitCount);
	printf("\n\tbiCompression   \t%d", 	(ptrbitmap->bmih).biCompression);
	printf("\n\tbiSizeImage   	\t%d", 	(ptrbitmap->bmih).biSizeImage);
	printf("\n\tbiXPelsPerMeter \t%d", 	(ptrbitmap->bmih).biXPelsPerMeter);
	printf("\n\tbiYPelsPerMeter \t%d", 	(ptrbitmap->bmih).biYPelsPerMeter);
	printf("\n\tbiClrUsed   	\t%d", 	(ptrbitmap->bmih).biClrUsed);
	printf("\n\tbiClrImportant  \t%d", 	(ptrbitmap->bmih).biClrImportant);
#endif

}


/*****************************************************************************

Routine: ReadBMPFrame

returns        	: void
Description			: Reads BMP pixels from Input FIle
*****************************************************************************/
void ReadBMPFrame(bmpCodecObject *ptrbitmap, UINT8 *aRGBPixels, FILE *pbmpfile)
{
	UINT32 width, height;
	UINT32 offset;
	UINT8 *pRowRGB;

	UINT32 j;

	offset = (ptrbitmap->bmfh).bfOffbits;
	width = (ptrbitmap->bmih).biWidth;
	height = (ptrbitmap->bmih).biHeight;

	fseek(pbmpfile, offset, SEEK_SET);
	for(j=0; j < height; j++)
	{
		pRowRGB = aRGBPixels + (height - j -1)*width*3;
		fread(pRowRGB, sizeof(UINT8), width*3, pbmpfile);
	}

}


